package admin

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/admin/adminLogic"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/database"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	pbAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/admin"
)

func (rpc *admin) AdminOperationLogDel(ctx context.Context, pb *pbAdmin.AdminOperationLogDelReq) (*pbAdmin.AdminOperationLogDelResp, error) {

	criteria := map[string]interface{}{
		"id": pb.Id,
	}

	if pb.OperaterId != 0 {
		adminIds := adminLogic.GetChildrenRoleAdminIds(ctx, pb.OperaterId, true)
		if len(adminIds) != 0 {
			criteria["admin_id"] = adminIds
		}
	}

	rowCount, dbErr := database.Db.GetDefaultDb(ctx).Delete("admin_operation_log", criteria)

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	return &pbAdmin.AdminOperationLogDelResp{
		RowCount: rowCount,
	}, nil
}
